package com.example.tnot.Teacher.CoursePart.adapter;

import static com.example.tnot.Teacher.CoursePart.adapter.rvMyTeachAdapter.BASE_IMAGE_URL;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.ItemBean.rv_MyStudyListItem;
import com.example.tnot.Teacher.CoursePart.ItemBean.rv_MyTeachListItem;

import java.util.List;

public class rvMyStudyAdapter extends RecyclerView.Adapter<rvMyStudyAdapter.ViewHolder>{
    private List<rv_MyStudyListItem> StudyCourse;
    public rvMyStudyAdapter(List<rv_MyStudyListItem> courses) {
        StudyCourse = courses;
    }

    @NonNull
    @Override
    public rvMyStudyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_rv_teacher_study_course, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull rvMyStudyAdapter.ViewHolder holder, int position) {
            rv_MyStudyListItem item = StudyCourse.get(position);
            String courseId = item.getCourse_id();
            String courseName = item.getCourse_name();
            String imageUrl = BASE_IMAGE_URL + courseId;

        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.example_face)
                .error(R.drawable.example_face)
                .transform(new CenterCrop(), new RoundedCorners(8));

        Glide.with(holder.iv_CourseImage.getContext())
                .load(imageUrl)
                .apply(options)
                .into(holder.iv_CourseImage);

            holder.tv_CourseName.setText(StudyCourse.get(position).getCourse_name());
            holder.tv_CourseTeacher.setText(StudyCourse.get(position).getCourse_teacher());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NavController navController = Navigation.findNavController(v);
                    Bundle bundle = new Bundle();
                    bundle.putString("courseName", courseName);
                    bundle.putString("jumpType", "学习");
                    navController.navigate(R.id.action_courseContainerFragment_to_classDetailFragment,bundle);
                }
            });
    }

    @Override
    public int getItemCount() {
        return StudyCourse.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tv_CourseName, tv_CourseTeacher;
        public ImageView iv_CourseImage;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            String URL = BASE_IMAGE_URL;
            tv_CourseName = itemView.findViewById(R.id.tv_CourseName);
            tv_CourseTeacher = itemView.findViewById(R.id.tv_CourseTeacher);
            iv_CourseImage = itemView.findViewById(R.id.iv_CourseImage);
        }
    }
    public static class MarginItemDecoration extends RecyclerView.ItemDecoration {
        private int marginleft;
        private int marginright;
        private int margintop;
        private int marginbottom;
        public MarginItemDecoration(int margintop, int marginbottom,int marginleft, int marginright) {
            this.marginleft = marginleft;
            this.marginright = marginright;
            this.margintop = margintop;
            this.marginbottom = marginbottom;

        }
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
             outRect.left = marginleft;
             outRect.right = marginright;
             outRect.top = margintop;
            int position = parent.getChildLayoutPosition(view);
            if (position != state.getItemCount() - 1) {
                outRect.bottom = marginbottom;
            }
        }
    }
}
